﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Mode1
{
    public partial class Login : Form
    {
        public string userName;//用户账号
        public string pwd;//用户密码
        string threshould = "";//控制阀值（焦点失去&获取）
        public Login()
        {
            InitializeComponent();
        }

        private void tbUserName_TextChanged(object sender, EventArgs e)
        {

        }

        private void tbUserName_Click(object sender, EventArgs e)
        {
            this.tbUserName.Clear();//清空文本框
        }
        /// 账号文本框焦点获取事件
        private void tbUserName_Enter(object sender, EventArgs e)
        {
            if (this.tbUserName.TextLength == 0)
            {
                this.tbUserName.Text = "输入账号";
            }
            else
            {
                userName = this.tbUserName.Text.Trim();
            }
        }

        private void tbUserName_Leave(object sender, EventArgs e)
        {
            if (tbUserName.TextLength == 0)
                this.tbUserName.Text = "输入账号";
        }

        private void tbPassword_Click(object sender, EventArgs e)
        {
            this.tbPassword.Clear();
        }
        /// 密码输入框挡焦点失去事件
        private void tbPassword_Leave(object sender, EventArgs e)
        {
            if (tbPassword.TextLength == 0)
            {
                this.tbPassword.PasswordChar = default(char);
                this.tbPassword.Text = "输入密码";
            }
            else
            {
                this.tbPassword.PasswordChar = '*';//遮挡密码
                pwd = tbPassword.Text.Trim(); //获取密码框输入值给变量pwd
            }

        }

        private void tbPassword_Enter(object sender, EventArgs e)
        {
            this.tbPassword.PasswordChar = '*';//遮挡密码
            this.tbPassword.Text = tbPassword.Text.Trim();//获取密码 Trim()去除字符串挡空格
        }
        //用户文本框事件处理
        private void UserNameEvent()
        {
            switch (threshould)
            {
                case "Enter":
                    if (this.tbUserName.Text == "输入账号")
                        this.tbUserName.Clear();//清空文本框
                    break;
                case "Leave":
                    if (this.tbUserName.TextLength == 0)
                        this.tbUserName.Text = "输入账号";
                    break;
            }
            if (this.tbUserName.TextLength != 0 || this.tbUserName.Text != "输入账号")
                userName = this.tbUserName.Text.Trim();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void ClearText()
        {
            foreach (Control Ctrol in this.Controls)
            {
                if (Ctrol is TextBox)
                {
                    Ctrol.Text = "";
                }
            }
        }

        /// <summary>
        /// 取消按钮单击事件
        /// </summary>

        private void tbCancel_Click(object sender, EventArgs e)
        {
            ClearText();//调用ClearText（）方法清空文本框内容
            this.tbPassword.PasswordChar = default(char);//密码文本框PasswordChar恢复默认值
            this.tbUserName.Text = "输入账号";
            this.tbPassword.Text = "输入密码";
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (userName.Length == 0)
            {
                MessageBox.Show("请输入用户名！", "系统提示");
                this.tbUserName.Focus();//焦点定位
                return;
            }
            if (pwd.Length == 0)
            {
                MessageBox.Show("请输入密码!", "系统提示");
                this.tbPassword.Focus();
                return;
            }
            if (userName.Length == 0 && pwd.Length == 0)
            {
                MessageBox.Show("用户登录或密码不能为空", "系统提示");
                return;
            }
            BusinessLogic logic = new BusinessLogic();
            if (logic.UserLogin(userName, pwd))
            {
                MessageBox.Show("恭喜你，你已经登录成功！", "系统提示");
            }
            else
            {
                MessageBox.Show("用户名或密码错误，请验证后重新登录！", "系统提示");
            }

        }
    }
}
